package com.annotation;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.model.Msg;
import com.dfec.utils.Consists;
import com.dfec.utils.ResponseUtil;

/**
 * Servlet implementation class BaseSeverlet
 */
public class BaseSeverlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	   List<Object> list=null;
       Msg msg=null;
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	   invoke(request,response);
    }
    /**
     * 执行指定的方法
     * @param request
     * @param response
     * @throws IOException
     */
    protected void invoke(HttpServletRequest request, HttpServletResponse response) throws IOException{
        Class<?> clz =  this.getClass();
        int count=0;
        boolean flag = true;
        
        
//        Field[] fields = clz.getDeclaredFields();
//        for (Field field : fields) {
//        	 if(field.isAnnotationPresent(ServiceAnnotation.class)){
//        		 if(field.toString().contains("Service")) {
//	        		 try {
//	        			 ServiceAnnotation cls = field.getAnnotation(ServiceAnnotation.class);
//	        			 Class<?> cla = cls.name();
//	        				 field.setAccessible(true);
//	            			 field.set("testService", cla.newInstance());	 
//	        			 
////	        			System.out.println(field.getName()+"????????????"+cls.name());
//	                 } catch (Exception e) {
//	                     e.printStackTrace();
//	                 }
//        		 }
//        	 }
//		}
//        
        String method = request.getParameter("m");
        Method[] methods = clz.getDeclaredMethods();
        for(Method m : methods){
            if(m.isAnnotationPresent(MethodAnnotation.class)){
                MethodAnnotation methodAnnotation = m.getAnnotation(MethodAnnotation.class);
                if(methodAnnotation.method().equals(method)){
                    try {
                        m.invoke(this, request,response);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    flag = true;
                    break;
                }else{
                    flag = false;
                }
            }else{
                count++;
            }
        }
        
        
      
     
        if(!flag){
        	list=new ArrayList<>();
            msg=new Msg();
    		    msg.setCode(Consists.DATA_Code_OK__NODATA);
	        	msg.setMsg("method中的参数是否配置出错");
	        	list.add(msg);
	        	ResponseUtil.sendScript(response, JSON.toJSONString(list));
            return;
        }
        if(count==methods.length){
            msg.setCode(Consists.DATA_Code_OK__NODATA);
        	msg.setMsg("有添加任何方法映射");
        	list.add(msg);
        	ResponseUtil.sendScript(response, JSON.toJSONString(list));
            return;
        }

    }
}
